package com.qf.ssm.service.impl;

import com.github.pagehelper.PageHelper;
import com.qf.ssm.common.exception.ServiceException;
import com.qf.ssm.common.request.BlogAppRequestParams;
import com.qf.ssm.common.vo.ResponseCode;
import com.qf.ssm.entity.Blog;
import com.qf.ssm.mapper.BlogAppMapper;
import com.qf.ssm.service.BlogAppService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author ISALYL
 * @create 2021-10-13 17:33
 */
@Service
public class BlogAppServiceImpl implements BlogAppService {
    @Resource
    private BlogAppMapper blogAppMapper;

    @Override
    public Boolean writeCommonBlog(BlogAppRequestParams params) {
        Blog blog = new Blog();
        BeanUtils.copyProperties(params,blog);
        Integer result = blogAppMapper.insertCommonBlog(blog);
        if (result >0){
            return true;
        }else {
            throw new ServiceException(ResponseCode.ERROR);
        }
    }

    @Override
    public Boolean deleteBlog(Long blogId) {
        Integer result = blogAppMapper.deleteBlog(blogId);
        if (result >0){
            return true;
        }else {
            throw new ServiceException(ResponseCode.ERROR);
        }
    }

    @Override
    public List<Blog> findCommonBlog(Integer page, Integer size) {
        Blog blog = new Blog();
        PageHelper.startPage(page, size);
        try {
            List<Blog> blogs = blogAppMapper.selectBlog(blog);
            return blogs;
        } catch (ServiceException e) {
            throw new ServiceException(ResponseCode.ERROR);
        }
    }

}
